/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

/**
 *
 * @author Van persie
 */
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import MaterialPack.*;
import EntityGameObject.*;
import Supporter.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.obex.HeaderSet;
/**
 *
 * @author Van persie
 */
public  class PetManagement extends AbstractCanvas
{
    private Image bufferGroup;
    private Vector groups;
    private boolean  isChanging = false;
    private int currentGrp;
    private int shrinkGrp;
    private int directChanging;
    private int xGroupPos;
    private int yGroupPos;
    private int widthGroup;
    private int heightGroup;
    
    // Control
    Button SpeedBtn = null;
    InfoPanel SpeedPnl = null;
    Button TongueLenBtn = null;
    InfoPanel TonguePnl = null;
    
    Button toPlayBtn = null;
    
    // animation
    private Timer timer ;        
    private Sticker sticker ;

    // Pet 
    PetInstance petInstance = null;
    Image Rule = null;
    InfoPanel MoneyLabel = null;
    InfoPanel MoneyVal = null;
    
    InfoPanel PointsLabel = null;
    InfoPanel PointsVal = null;
    
    InfoBar SpeedBar = null;
    
    int scalar = 40;
        
    // buffer 
    Vector listBackObjs;
    Image BufferImg = null;
    Graphics BufferGraphics = null;
 
    public PetManagement(MainMIDlet midlet)
    { 
        
        main = midlet;
        height = this.getHeight();
        width = this.getWidth();
        
        listBackObjs = new Vector();
        BufferImg = Image.createImage(width, height);
        BufferGraphics = BufferImg.getGraphics();
        try{
            LoadResource( );            
        }catch(Exception e){}
        
        timer = new Timer();                
        
        //StartTimer();
        
    }
    
    public void LoadManagementGroups(ImageSupporter imgSupporter) {
        //group 
        
        xGroupPos =  width/4 - 15;
        yGroupPos = height/4 + 10;
        widthGroup = 3*width/4 + 10 ;
        heightGroup = 3*width/4 + 20;
        int wid = width/20 ;
        
        
        bufferGroup = ImageSupporter.alphaBlend(ImageSupporter.scaleImage(imgSupporter.getImage("/Plane.png"), widthGroup, heightGroup));
 
// SKILL
        groups.addElement(new SubGroup("/Plane.png","Skill ",xGroupPos, yGroupPos,widthGroup , heightGroup));
        listObjects.addElement(groups.lastElement());
        
        
        PointsLabel = new InfoPanel(null, "Points : ",xGroupPos + wid , yGroupPos + wid, 80, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_LARGE));
            PointsLabel.setNoBackground(true);
            PointsLabel.setAnchorMode(InfoPanel.ANC_MIDDLE);
        PointsVal = new InfoPanel("/Disk.png", Integer.toString(petInstance.GetAttributes(Pet.POINTS)) ,PointsLabel.getxPos() + PointsLabel.getWidth() + wid , yGroupPos + wid, scalar, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
            PointsVal.SetAlphaBack();
            PointsVal.TransBackground(true);
            PointsVal.setAnchorMode(InfoPanel.ANC_MIDDLE);
        
        ((SubGroup)listObjects.lastElement()).Add(PointsLabel);
        ((SubGroup)listObjects.lastElement()).Add(PointsVal);
        
        InfoPanel temp = null;
        SpeedBtn = new Button("/PlusBtn1.png", "/PlusBtn2.png","/PlusBtn2.png", null, xGroupPos + wid , yGroupPos + wid + scalar, scalar, scalar);
        
        temp = new InfoPanel(null, " Speed : ", SpeedBtn.getxPos() + SpeedBtn.getWidth() + wid /2, SpeedBtn.getyPos() + SpeedBtn.getHeight() - 3*scalar/4, 3*scalar/2, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
            temp.setNoBackground(true);
            temp.setAnchorMode(InfoPanel.ANC_LEFT);
        
        SpeedPnl = new InfoPanel("/Disk.png", Integer.toString(petInstance.getSpeech()),temp.getxPos() + 19*scalar/10,SpeedBtn.getyPos() + SpeedBtn.getHeight() - 9*scalar/10, scalar, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        SpeedPnl.SetAlphaBack();
        SpeedPnl.TransBackground(true);
        
        ((SubGroup)listObjects.lastElement()).Add(SpeedBtn);
        ((SubGroup)listObjects.lastElement()).Add(temp);           
        ((SubGroup)listObjects.lastElement()).Add(SpeedPnl);
        
        TongueLenBtn = new Button("/PlusBtn1.png", "/PlusBtn2.png","/PlusBtn2.png", null, xGroupPos + wid , yGroupPos + 2*(wid + scalar), scalar, scalar);
        temp = new InfoPanel(null, " Tongue : ", TongueLenBtn.getxPos() + TongueLenBtn.getWidth() + wid/2 , TongueLenBtn.getyPos() + TongueLenBtn.getHeight() - 3*scalar/4, 2*scalar, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
            temp.setNoBackground(true);
            temp.setAnchorMode(InfoPanel.ANC_LEFT);
            
        TonguePnl = new InfoPanel("/Disk.png", Integer.toString(petInstance.getTongueLen()), temp.getxPos() + 19*scalar/10,TongueLenBtn.getyPos() + TongueLenBtn.getHeight() - 9*scalar/10, scalar, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        TonguePnl.SetAlphaBack();
        TonguePnl.TransBackground(true);
        ((SubGroup)listObjects.lastElement()).Add(TongueLenBtn);
        ((SubGroup)listObjects.lastElement()).Add(temp);           
        ((SubGroup)listObjects.lastElement()).Add(TonguePnl);
        
        
        
//       SHOP
        groups.addElement(new SubGroup("/Plane.png","SHOP",xGroupPos, yGroupPos,widthGroup , heightGroup));
        listObjects.addElement(groups.lastElement());
        
        //((SubGroup)listObjects.lastElement()).Add(new Button("/PlusBtn1.png", "/PlusBtn2.png","/PlusBtn2.png", null, 2*width/3 - scalar, height/3, scalar*2/3, scalar*2/3));
        MoneyLabel = new  InfoPanel(null, "Money : ", xGroupPos + 10, yGroupPos + 10, 2*scalar, scalar,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        MoneyLabel.setNoBackground(true);
        MoneyVal = new  InfoPanel("/Disk.png", Integer.toString(petInstance.GetAttributes(Pet.MONEY)), xGroupPos + 10 + 2*scalar, yGroupPos + 10, scalar,2*scalar/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        MoneyVal.TransBackground(true);
        ((SubGroup)listObjects.lastElement()).Add(MoneyLabel);
        ((SubGroup)listObjects.lastElement()).Add(MoneyVal);
        ((SubGroup)listObjects.lastElement()).Add(new PetItem("/NormalSound.png", "Home",xGroupPos + 10, yGroupPos + 10, scalar , scalar,PetItem.TYPE_CHECK));
        ((SubGroup)listObjects.lastElement()).Add(new PetItem("/NormalSound.png", "River",xGroupPos + 10, yGroupPos + 10, scalar , scalar,PetItem.TYPE_CHECK));
        ((SubGroup)listObjects.lastElement()).Add(new PetItem("/NormalSound.png", "Night",xGroupPos + 10, yGroupPos + 10, scalar , scalar,PetItem.TYPE_CHECK));
        ((SubGroup)listObjects.lastElement()).Add(new PetItem("/NormalSound.png", "Garden",xGroupPos + 10, yGroupPos + 10, scalar , scalar,PetItem.TYPE_CHECK));
        ((SubGroup)groups.lastElement()).ArrangeItem();
        currentGrp = 0;
        ((SubGroup)groups.elementAt(currentGrp)).setIsVisible(true);
    }

    public void LoadPet() {
        // Pet photo
        petInstance = new PetInstance("/PetPhoto.png", 2, 2, width/3, height/4,main.getPet());
        
        //listObjects.addElement(petInstance);
        listBackObjs.addElement(petInstance);
    }
    
    public void LoadPetInfo()
    {
        // example
        int exp = 50;       // 50%
        int speed = 70;     // 70%
        int accuract = 80;  // %
        int level = 2;      //  count
        
        
        int x = width/3 + 10;
        int y = 30;
        // set up

        
        InfoPanel temp;
        
        temp = new InfoPanel(null, "Level " + Integer.toString(petInstance.GetAttributes(Pet.LEVEL)),petInstance.getxPos() + petInstance.getWidth() + 10, height/100, scalar*2, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_LARGE));
        temp.setNoBackground(true);
        temp.setAnchorMode(InfoPanel.ANC_LEFT);
        listBackObjs.addElement(temp);
        
        
        temp = new InfoPanel(null, "Speed ",petInstance.getxPos() + petInstance.getWidth() + 10, height/80 + scalar/2 , 2*scalar, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        temp.setNoBackground(true);
        temp.setAnchorMode(InfoPanel.ANC_LEFT);
        listBackObjs.addElement(temp);
        
        SpeedBar = new InfoBar(null, InfoBar.HORIZON, petInstance.GetAttributes(Pet.SPEED)*10, x + 5 , y + 15 , 140, 8);
        SpeedBar.SetColor(255, 0, 0);

        listBackObjs.addElement(SpeedBar);
     
        
    }
public void LoadResource() throws IOException {
       if(listObjects != null)
            listObjects.removeAllElements();
        listObjects = new Vector();
        groups = new Vector();
        ImageSupporter imgSupporter = new ImageSupporter();
        //background = ImageSupporter.scaleImage(  Image.createImage("/Background.png"),width,height);
        Image temp = imgSupporter.getImage("/Background.png");
        background = ImageSupporter.scaleImage(temp, width, height);
        Rule = ImageSupporter.scaleImage(imgSupporter.getImage("/Rule.png"), 40, 2*height/3 ); 
        
        //
        toPlayBtn = new Button("/Forward.png","/ForwardPressed.png", "/ForwardPressed.png", null, 3*width/4,9*height/10, width/4, height/10);
        //listBackObjs.addElement(toPlayBtn);
        // Load pet before Groups
        LoadPet();
        
        LoadManagementGroups(imgSupporter);      
        
        LoadPetInfo();
        
        BufferImg = Image.createImage(width, height);
        BufferGraphics = BufferImg.getGraphics();
        
        paintBackground();
        
    }
    public void ShowPetInfo() {
        PointsVal.setText(Integer.toString(petInstance.GetAttributes(Pet.POINTS)));
        SpeedPnl.setText(Integer.toString(petInstance.GetAttributes(Pet.SPEED)));
        SpeedBar.setRatio(petInstance.GetAttributes(Pet.SPEED)*10);
        TonguePnl.setText(Integer.toString(petInstance.GetAttributes(Pet.TONGUELEN)));
        MoneyVal .setText(Integer.toString(petInstance.GetAttributes(Pet.MONEY)));
        paintBackground();
    }

    public void StartTimer() {
        sticker = new Sticker();        
        timer.schedule(sticker, 50,50);
        isChanging = true;
    }
    public void PauseTimer()
    {
        sticker.cancel();
        isChanging = false;
    }
    
        
    class Sticker extends TimerTask
        {
            public void run()
            {
                TimerStick();
                repaint();
                
            }
        }
        
   
        
    
    protected void keyReleased(int key)
    {
        int n = groups.size();
        switch(key)
        {
            case KEY_NUM4:
            case LEFT:
                if(isChanging)
                    Skip();
                shrinkGrp = currentGrp;
                currentGrp = (currentGrp - 1 + n)%n;
                Change(Graphics.LEFT);  
                break;
               
            case KEY_NUM6:
            case RIGHT:
                if(isChanging)
                    Skip();
                shrinkGrp = currentGrp;
                currentGrp = (currentGrp + 1)%n;
                Change(Graphics.RIGHT);
                
                break;
            case KEY_NUM0:
                main.exitMIDlet();
                break;
        }
        repaint();
    }
    public void Skip()
    {
        ((SubGroup)groups.elementAt(shrinkGrp)).setIsVisible(false);
        ((SubGroup)groups.elementAt(currentGrp)).setIsVisible(true);
        isChanging = false;
    }
    public void Change(int direction) {
                
        ((SubGroup)groups.elementAt(shrinkGrp)).Back();        
        ((SubGroup)groups.elementAt(currentGrp)).Front();
        directChanging = direction;
        StartTimer();
    }
    protected void sizeChanged(int w, int h)
    {
        width = w;
        height = h;
        try{
            LoadResource( );            
        }catch(Exception e){}
        
    }
    protected void pointerDragged(int x, int y)
    {
         for(int i=0; i <listObjects.size(); i++)
         {
             ((MaterialObject)listObjects.elementAt(i)).Drag(x, y);             
         
         }
         paintBackground();
         repaint();
    }

    protected void pointerPressed(int x, int y)
    {
         
         for(int i=0; i <listObjects.size(); i++)
             ((MaterialObject)listObjects.elementAt(i)).Press(x,y);
         for(int j=0; j <listBackObjs.size(); j++)
             ((MaterialObject)listBackObjs.elementAt(j)).Press(x,y);
         toPlayBtn.Press(x, y);
         paintBackground();
         repaint();
    }
    protected void pointerReleased (int x, int y)
    {
//        for(int i=0; i <listObjects.size(); i++)
//            ((MaterialObject)listObjects.elementAt(i)).Release(x,y);
        if(toPlayBtn.Release(x, y))
        {
            //PauseTimer();
            // switch to play
        }
        
        

        if(SpeedBtn.Release(x, y) && petInstance.GetAttributes(Pet.POINTS) > 0 )
        {
            petInstance.AddAttributes(Pet.SPEED, 1);
            petInstance.AddAttributes(Pet.POINTS, -1);
            ShowPetInfo();
            
        }
        
        if(TongueLenBtn.Release(x, y) && petInstance.GetAttributes(Pet.POINTS) > 0)
        {
            petInstance.AddAttributes(Pet.TONGUELEN, 1);
            petInstance.AddAttributes(Pet.POINTS, -1);
            ShowPetInfo();
        }

        if(petInstance.Release(x, y))
        {
            // draw something
            paintBackground();            
            // do something
            main.SwitchTo(MainMIDlet.SCENE_PLAYPET);
        }
        SubGroup grp = null;
        for(int i=0; i <groups.size(); i++)        
        {
            grp = ((SubGroup)listObjects.elementAt(i));
            grp.Release(x,y);
            if(grp.getItems().size() > 0)
            {
                for(int j = 0; j <grp.getItems().size() ; j++ )
                    if(((PetItem)grp.getItems().elementAt(j)).Release(x, y)  )
                    {
                        if(((PetItem)grp.getItems().elementAt(j)).isActived() == false )
                        {
                            if(petInstance.GetAttributes(Pet.MONEY) > ((PetItem)grp.getItems().elementAt(j)).getPrice())
                            {
                                petInstance.BuyItem(((PetItem)grp.getItems().elementAt(j)));
                                ((PetItem)grp.getItems().elementAt(j)).setActived(true);
                            }
                        }
                        else 
                        {
                            petInstance.ReturnItem(((PetItem)grp.getItems().elementAt(j)));
                            ((PetItem)grp.getItems().elementAt(j)).setActived(false);
                        }
                        ShowPetInfo();
                    }
            }
        }
        repaint();
    }
    protected void keyPressed(int keyCode)
    {
        
    }

    public void commandAction(Command command, Displayable displayable)
    {
        
    }
    
    public void TimerStick()
    {        
        
        boolean bool1 = ((SubGroup)groups.elementAt(currentGrp)).Expand(directChanging);
        boolean bool2 = ((SubGroup)groups.elementAt(shrinkGrp)).Shrink(directChanging);
        if(bool1 == true && bool2 == true)
        {
            PauseTimer();} 
        repaint();
    }
    public void paint(Graphics g )
    {
        g.drawImage(BufferImg, 0, 0, Graphics.TOP | Graphics.LEFT);
        for(int i=0; i <listObjects.size(); i++)
            ((MaterialObject)listObjects.elementAt(i)).paint(g);
        toPlayBtn.paint(g);
    }
    public void paintBackground()
    {
        BufferGraphics.drawImage(background, 0, 0,Graphics.TOP | Graphics.LEFT);
        BufferGraphics.drawImage(Rule, petInstance.getxPos(), petInstance.getyPos() + petInstance.getHeight() + 5, Graphics.TOP | Graphics.LEFT);
//        if(groups.size() > 1)
//            BufferGraphics.drawImage(bufferGroup, xGroupPos + 5, yGroupPos + 5, Graphics.TOP | Graphics.LEFT);
//        //for(int i=0; i <listObjects.size(); i++)
//            ((MaterialObject)listObjects.elementAt(i)).paint(g);
        for(int i=0; i <listBackObjs.size(); i++)
            ((MaterialObject)listBackObjs.elementAt(i)).paint(BufferGraphics);
    }
    public void run()
    {
        
    }
}
